'''
Created on 28/03/2010

@author: miguel
'''
from vec2d import vec2d
class GameBoard():
    (FREE, TREE, USER_BUG, USER_HOME, COM_BUG, COM_HOME, USER_BOX, COM_BOX) = range (8)
    board = [[FREE for col in range(19)] for row in range(20)]

    def __init__(self):
        for row in range(20):
            for col in range(19):
                self.board[row][col] = self.FREE

    def is_position_available(self, position, id):
        x = position.x / 30
        y = position.y / 30
        '''is the bug out of the field?'''
        if (x < 0 or x > 18) or (y < 0 or y > 19):
            return False
        else:
            pass

        '''is position busy?'''
        if self.USER_BUG == id:
            if self.board[y][x] == self.FREE or self.board[y][x] == self.USER_HOME or self.board[y][x] == self.USER_BOX or self.board[y][x] == self.COM_BOX:
                return True
            else:
                return False
        elif self.COM_BUG == id:
            if self.board[y][x] == self.FREE or self.board[y][x] == self.COM_HOME  or self.board[y][x] == self.USER_BOX or self.board[y][x] == self.COM_BOX:
                return True
            else:
                return False

    def take_position(self, position, id):
        x = position.x / 30
        y = position.y / 30
        #if self.board[y][x] == self.FREE:
        self.board[y][x] = id
        #else:
        #    print 'square busy'

    '''deprecated
    def release_position(self, position):
        x = position.x / 30
        y = position.y / 30
        self.board[y][x] = self.FREE'''

    def get_info_position(self, position):
        x = position.x / 30
        y = position.y / 30
        return self.board[y][x]

    def restore_position(self, position, original_id):
        x = position.x / 30
        y = position.y / 30
        self.board[y][x] = original_id

    def print_board(self):
        codes = ['FREE     ', 'TREE     ', 'USER_BUG ', 'USER_HOME', 'COM_BUG  ', 'COM_HOME ', 'USER_BOX ', 'COM_BOX  ']
        for row in range(20):
            for col in range(19):
                print codes[self.board[row][col]],
            print ''

if __name__ == '__main__':
    board = GameBoard()
    board.take_position(vec2d(30, 30), board.COM_BUG)
    board.take_position(vec2d(0, 0), board.COM_HOME)
    board.print_board()